package cn.innosmart.aq.manager;

import android.os.Message;
import android.util.Log;

import com.tutk.p2p.ConnectionEntity;

import org.json.JSONObject;

import java.util.ArrayList;

import cn.innosmart.aq.view.activity.BoxDetailActivity;

/**
 * Created by David on 2015/6/30.
 */
public class BaseManager {
    private String Tag = "BaseManager";

    public void log(String tag, String msg) {
        Log.v(tag, msg);
    }

    public boolean SendCommand(ConnectionEntity connectionEntity, String UID, String method, String params, long callid) {
        if (!isDeviceConnected(connectionEntity)) {
            log(Tag, "Device connect is wrong.");
            return false;
        }
        try {
            // 构造指令
            JSONObject jRoot = new JSONObject();
            jRoot.put("type", "req");
            jRoot.put("method", method);
            jRoot.put("callid", callid);
            JSONObject jParam = new JSONObject(params);
            jRoot.put("params", jParam);
            // 发送指令
            String msg = jRoot.toString();
            countDownMethod(connectionEntity, callid);
            connectionEntity.sendMessage(UID, msg);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    public boolean SendCommand(ConnectionEntity connectionEntity, String UID, String method, String params, long callid,String topic) {
        if (!isDeviceConnected(connectionEntity)) {
            log(Tag, "Device connect is wrong.");
            return false;
        }
        try {
            // 构造指令
            JSONObject jRoot = new JSONObject();
            jRoot.put("type", "req");
            jRoot.put("method", method);
            jRoot.put("callid", callid);
            JSONObject jParam = new JSONObject(params);
            jRoot.put("params", jParam);
            // 发送指令
            String msg = jRoot.toString();
            countDownMethod(connectionEntity, callid);
            connectionEntity.sendMessage(UID, msg,topic);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    private void countDownMethod(ConnectionEntity connectionEntity, long callid) {
        if (connectionEntity.countdowns == null) {
            connectionEntity.countdowns = new ArrayList<String>();
        }
        connectionEntity.countdowns.add(String.valueOf(callid));
        Message countdownMessage = BoxDetailActivity.getHandler().obtainMessage();
        countdownMessage.what = 1001;
        countdownMessage.obj = String.valueOf(callid);
        BoxDetailActivity.getHandler().sendMessageDelayed(countdownMessage, 20000);
    }

    public boolean SendNotify(ConnectionEntity connectionEntity, String name, String data,
                              String topic) {
        log(Tag, "SendNotify. name = " + name + ", data = " + data);
        if (!isDeviceConnected(connectionEntity)) {
            log(Tag, "Device connect is wrong.");
            return false;
        }
        try {
            // 构造指令
            JSONObject jRoot = new JSONObject();
            jRoot.put("type", "notif");
            jRoot.put("name", name);
            JSONObject jData = new JSONObject(data);
            jRoot.put("data", jData);

            // 发送指令
            String msg = jRoot.toString();
            connectionEntity.sendNotify(msg, topic);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return true;
    }

    private boolean isDeviceConnected(ConnectionEntity connectionEntity) {
        return true;
    }

}
